package com.zgkxzx.vediotest.page

import android.os.Bundle
import android.os.Environment
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.zgkxzx.vediotest.nativeapi.NativeApi
import com.zgkxzx.vediotest.utils.Constants
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch

class ClipActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            rootView()
        }
        NativeApi.hello()
    }

    @Composable
    private fun rootView() {
        Column {
            button("裁剪画幅视频1") {
                clip()
            }
            button("裁剪画幅视频2") {
                clip2()
            }
            button("裁剪画幅视频3") {
                clip3()
            }
        }
    }

    private fun clip() {
        GlobalScope.launch {
//            val clipResult = NativeApi.clip(
//                Constants.getHandleVideoPath(this@ClipActivity, "test"),
//                Constants.getHandleVideoPath(this@ClipActivity, "clip"),
//                430,
//                320
//            );
            val clipResult = NativeApi.clip(
                Constants.getHandleVideoPath(this@ClipActivity, "test"),
                Constants.getHandleVideoPath(this@ClipActivity, "yuv"),
                430,
                320
            );
            Log.d(TAG, "clip result:$clipResult")
        }

    }

    private fun clip2() {
        GlobalScope.launch {
//            val clipResult = NativeApi.clip(
//                Constants.getHandleVideoPath(this@ClipActivity, "test"),
//                Constants.getHandleVideoPath(this@ClipActivity, "clip"),
//                430,
//                320
//            );
            val clipResult = NativeApi.clip2(
                Constants.getHandleVideoPath(this@ClipActivity, "test"),
                Constants.getHandleVideoPath(this@ClipActivity, "yuv"),
                430,
                320
            );
            Log.d(TAG, "clip result:$clipResult")
        }

    }

    private fun clip3() {
        GlobalScope.launch {
//            val clipResult = NativeApi.clip(
//                Constants.getHandleVideoPath(this@ClipActivity, "test"),
//                Constants.getHandleVideoPath(this@ClipActivity, "clip"),
//                430,
//                320
//            );
            val path = Environment.getExternalStorageDirectory().absolutePath+"/video_test_yuv.mp4";
            val clipResult = NativeApi.clip3(
                Constants.getHandleVideoPath(this@ClipActivity, "test"),
//                Constants.getHandleVideoPath(this@ClipActivity, "yuv")
                path,
                430,
                320
            );
            Log.d(TAG, "clip result:$clipResult")
        }

    }

    @Composable
    private fun button(text: String, click: () -> Unit) = Button(
        onClick = click, modifier = Modifier
            .padding(10.dp)
            .fillMaxWidth()
    ) {
        Text(text = text)
    }

    companion object {
        const val TAG = "ClipActivity"
    }
}